//
//  HTUtils.m
//  TruckUser
//
//  Created by Ting Han on 2017/11/14.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTUtils.h"
#import "AppDelegate.h"
#import "HTLoginVC.h"
#import "BaseNavigationController.h"
#import <AudioToolbox/AudioToolbox.h>
#import "HTTruckTypeModel.h"
#import "HTAreaModel.h"

@implementation HTUtils
+ (void)reLoginVC{
    [HTMUserDefaultUtil setLginOuted];
    [HTMUserDefaultUtil setUserPhone:@""];
    UIApplication *app =[UIApplication sharedApplication];
    AppDelegate *app2 = (AppDelegate *)app.delegate;
    HTLoginVC *vc = [[HTLoginVC alloc]init];
    app2.window.rootViewController = [[BaseNavigationController alloc] initWithRootViewController:vc];
}
#pragma mark --- 网络请求
+ (void)netLoadNotification{
    NSData *deviceToken = [HTMUserDefaultUtil readDeviceToken];
    NSString * str  =[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]stringByReplacingOccurrencesOfString: @">" withString: @""]stringByReplacingOccurrencesOfString: @" " withString: @""];
    DBLog(@"友盟deviceToken:%@",str);
    if (str == nil || str.length <= 0) {
        return;
    }
    [HTHttpRequestManager requestNotificationRegisterWithType:@"2" regitid:str success:^(id  _Nullable responseObject) {
        DBLog(@"注册友盟：%@",responseObject);
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
+ (void)playMusicArrive{
    SystemSoundID soundID;
    NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"notification_voice" ofType:@"mp3"];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
    //提示音 带震动
    AudioServicesPlayAlertSound(soundID);
}

+ (NSString *)sortLengthDataDisplayStringWithArray:(NSMutableArray *)selectedLengths{
    NSMutableArray *array = [NSMutableArray array];
    for (HTTruckTypeModel *model in selectedLengths) {
        if (!(model.truckTypeModelType == TruckTypeModelTypeUnlimited)) {
            [array addObject:model.title];
        }
    }
    if (array.count <= 0) {
        return @"不限车长";
    }
    [array sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 doubleValue] < [obj2 doubleValue]) {
            return NSOrderedAscending;
        } else if ([obj1 doubleValue] > [obj2 doubleValue]) {
            return NSOrderedDescending;
        }
        return NSOrderedSame;
    }];
    if (array.count >= 2) {
        return [NSString stringWithFormat:@"%@-%@米", array.firstObject, array.lastObject];
    }else{
        return [NSString stringWithFormat:@"%@米", array.firstObject];
    }
}
+ (NSString *)sortLengthDataStringWithArray:(NSMutableArray *)selectedLengths{
    NSString *displayString = @"";
    NSMutableArray *array = [NSMutableArray array];
    for (HTTruckTypeModel *model in selectedLengths) {
        if (!(model.truckTypeModelType == TruckTypeModelTypeUnlimited)) {
            [array addObject:model.title];
        }
    }
    if (array.count <= 0) {
        return @"0";
    }
    [array sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if ([obj1 doubleValue] < [obj2 doubleValue]) {
            return NSOrderedAscending;
        } else if ([obj1 doubleValue] > [obj2 doubleValue]) {
            return NSOrderedDescending;
        }
        return NSOrderedSame;
    }];
    for (NSString *title in array) {
        displayString = [NSString stringWithFormat:@"%@,%@",displayString,title];
    }
    return [displayString substringFromIndex:1];
}
+ (NSString *)sortLengthDisplayStringWith:(NSString *)length{
    if (length == nil || length.length <= 0 || [length isEqualToString:@"0"]) {
        return @"不限车长";
    }
    NSArray *array = [length componentsSeparatedByString:@","];
    if (array.count <= 0) {
         return @"不限车长";
    }else if (array.count == 1){
        return [NSString stringWithFormat:@"%@米",array.firstObject];
    }else{
        return [NSString stringWithFormat:@"%@-%@米",array.firstObject,array.lastObject];
    }
}
+ (NSString *)placeDisplayStringWith:(HTAreaModel *)areaModel{
    NSString *place = @"";
    if (areaModel.provinceString != nil && areaModel.provinceString.length > 0) {
        place = [NSString stringWithFormat:@"%@%@",place,areaModel.provinceString];
    }
    if (areaModel.cityString != nil && areaModel.cityString.length > 0) {
        place = [NSString stringWithFormat:@"%@%@",place,areaModel.cityString];
    }
    if (areaModel.areaType == HTAreaTypeArea) {
        place = [NSString stringWithFormat:@"%@%@",place,areaModel.title];
    }
    if (place == nil || place.length <= 0) {
        place = areaModel.title;
    }
    return place;
}
+ (NSString *)placePostStringWith:(HTAreaModel *)areaModel{
    NSString *place = @"";
    if (areaModel.provinceString != nil && areaModel.provinceString.length > 0) {
        place = [NSString stringWithFormat:@"%@%@",place,areaModel.provinceString];
    }
    if (areaModel.cityString != nil && areaModel.cityString.length > 0) {
        place = [NSString stringWithFormat:@"%@%@",place,areaModel.cityString];
    }
    if (areaModel.areaType == HTAreaTypeArea) {
        place = [NSString stringWithFormat:@"%@%@",place,areaModel.title];
    }
    if (place == nil || place.length <= 0) {
        place = areaModel.title;
    }
    return place;
}
#pragma mark -- 时间转换
/**
 2  *  计算上次日期距离现在多久
 3  *
 4  *  @param lastTime    上次日期(需要和格式对应)
 5  *  @param format1     上次日期格式
 6  *  @param currentTime 最近日期(需要和格式对应)
 7  *  @param format2     最近日期格式
 8  *
 9  *  @return xx分钟前、xx小时前、xx天前
 10  */
+ (NSString *)timeIntervalFromLastTime:(NSString *)lastTime lastTimeFormat:(NSString *)format1 ToCurrentTime:(NSString *)currentTime currentTimeFormat:(NSString *)format2{
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
     dateFormatter1.dateFormat = format1;
    NSDate *lastDate = [dateFormatter1 dateFromString:lastTime];
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
    dateFormatter2.dateFormat = format2;
    NSDate *currentDate = [dateFormatter2 dateFromString:currentTime];
    return [HTUtils timeIntervalFromLastTime:lastDate ToCurrentTime:currentDate];
}
+ (NSString *)timeIntervalFromLastTime:(NSString *)lastTime lastTimeFormat:(NSString *)format1{
    if (lastTime == nil || lastTime.length <= 0) {
        return @"";
    }
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
    dateFormatter1.dateFormat = format1;
    NSDate *lastDate = [dateFormatter1 dateFromString:lastTime];
    NSDate *now = [NSDate date];
    return [HTUtils timeIntervalFromLastTime:lastDate ToCurrentTime:now];
}
+ (NSString *)timeIntervalFromLastTime:(NSString *)lastTime{
    return [HTUtils timeIntervalFromLastTime:lastTime lastTimeFormat:@"YYYY-MM-dd HH:mm:ss"];
}
+ (NSString *)timeIntervalFromLastTime:(NSDate *)lastTime ToCurrentTime:(NSDate *)currentTime{
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSDate *lastDate = [lastTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:lastTime]];
    NSDate *currentDate = [currentTime dateByAddingTimeInterval:[timeZone secondsFromGMTForDate:currentTime]];
    NSInteger intevalTime = [currentDate timeIntervalSinceReferenceDate] - [lastDate timeIntervalSinceReferenceDate];
     NSInteger minutes = intevalTime / 60;
     NSInteger hours = intevalTime / 60 / 60;
     NSInteger day = intevalTime / 60 / 60 / 24;
     NSInteger month = intevalTime / 60 / 60 / 24 / 30;
     NSInteger yers = intevalTime / 60 / 60 / 24 / 365;
    if (minutes <= 1) {
        return  @"刚刚";
    }else if (minutes < 60){
        return [NSString stringWithFormat: @"%ld分钟",(long)minutes];
    }else if (hours < 24){
        return [NSString stringWithFormat: @"%ld小时",(long)hours];
    }else if (day < 30){
        return [NSString stringWithFormat: @"%ld天",(long)day];
    }else if (month < 12){
        NSDateFormatter * df =[[NSDateFormatter alloc]init];
        df.dateFormat = @"M个月";
        NSString * time = [df stringFromDate:lastDate];
        return time;
    }else if (yers >= 1){
        NSDateFormatter * df =[[NSDateFormatter alloc]init];
        df.dateFormat = @"yyyy年";
        NSString * time = [df stringFromDate:lastDate];
        return time;
    }
    return @"";
}

+ (NSString *)timeIntervalExchange:(NSString *)timeString{
  return  [HTUtils timeIntervalExchange:timeString TimeFormat: @"YYYY-MM-dd HH:mm:ss"];
}
+ (NSString *)timeIntervalExchange:(NSString *)timeString TimeFormat:(NSString *)format1{
    if (timeString == nil || timeString.length <= 0) {
        return @"";
    }
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
    dateFormatter1.dateFormat = format1;
    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT: (0 * 60 * 60)];
    [dateFormatter1 setTimeZone:timeZone];
    NSDate *lastDate = [dateFormatter1 dateFromString:timeString];
    return [self compareDate:lastDate];
}
+ (NSString *)compareDate:(NSDate *)date{
    
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *today = [[NSDate alloc] init];
    NSDate *beforeYesterday, *yesterday;
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMT];
    today = [today dateByAddingTimeInterval:interval];
    yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
    beforeYesterday = [yesterday dateByAddingTimeInterval: -secondsPerDay];
    NSString * todayString = [[today description] substringToIndex:10];
    NSString * yesterdayString = [[yesterday description] substringToIndex:10];
    NSString * beforeString = [[beforeYesterday description] substringToIndex:10];
    NSString * dateString = [[date description] substringToIndex:10];
    if ([dateString isEqualToString:todayString]){
        //今天
        NSDateFormatter * df =[[NSDateFormatter alloc]init];
        NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT: (0 * 60 * 60)];
        df.timeZone = zone;
        df.dateFormat = @"aaa HH:mm";
        NSString * time = [df stringFromDate:date];
        return time;
        
    } else if ([dateString isEqualToString:yesterdayString]){//昨天
        NSDateFormatter * df =[[NSDateFormatter alloc]init];
        NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT: (0 * 60 * 60)];
        df.timeZone = zone;
        df.dateFormat = @"HH:mm";
        NSString * time = [df stringFromDate:date];
        return [NSString stringWithFormat:@"昨天 %@",time];
    }else if ([dateString isEqualToString:beforeString]){//前天
        NSDateFormatter * df =[[NSDateFormatter alloc]init];
        NSTimeZone *zone =[NSTimeZone timeZoneForSecondsFromGMT: (0 * 60 * 60)];
        df.timeZone = zone;
        df.dateFormat = @"HH:mm";
        NSString * time = [df stringFromDate:date];
        return [NSString stringWithFormat:@"前天 %@",time];
    }else{
        NSDateFormatter * df =[[NSDateFormatter alloc]init];
        NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT: (0 * 60 * 60)];
        df.timeZone = zone;
        df.dateFormat = @"YYYY-MM-dd HH:mm";
        NSString * time = [df stringFromDate:date];
        return time;
    }
}


@end
